home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / php / PEAR / Pager / Jumping.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  9.4 KB  |  280 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Contains the Pager_Jumping class
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. The name of the author may not be used to endorse or promote products
  17.  *    derived from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  20.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22.  * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  23.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29.  *
  30.  * @category   HTML
  31.  * @package    Pager
  32.  * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
  33.  * @author     Richard Heyes <richard@phpguru.org>,
  34.  * @copyright  2003-2006 Lorenzo Alberton, Richard Heyes
  35.  * @license    http://www.debian.org/misc/bsd.license  BSD License (3 Clause)
  36.  * @version    CVS: $Id: Jumping.php,v 1.12 2006/04/18 20:01:47 quipo Exp $
  37.  * @link       http://pear.php.net/package/Pager
  38.  */
  39.  
  40. /**
  41.  * require PEAR::Pager_Common base class
  42.  */
  43. require_once 'Pager/Common.php';
  44.  
  45. /**
  46.  * Pager_Jumping - Generic data paging class  ("jumping window" style)
  47.  * Handles paging a set of data. For usage see the example.php provided.
  48.  *
  49.  * @category   HTML
  50.  * @package    Pager
  51.  * @author     Lorenzo Alberton <l dot alberton at quipo dot it>
  52.  * @author     Richard Heyes <richard@phpguru.org>,
  53.  * @copyright  2003-2005 Lorenzo Alberton, Richard Heyes
  54.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  55.  * @link       http://pear.php.net/package/Pager
  56.  */
  57. class Pager_Jumping extends Pager_Common
  58. {
  59.     // {{{ Pager_Jumping()
  60.  
  61.     /**
  62.      * Constructor
  63.      *
  64.      * @param array $options    An associative array of option names
  65.      *                          and their values
  66.      * @access public
  67.      */
  68.     function Pager_Jumping($options = array())
  69.     {
  70.         $err = $this->setOptions($options);
  71.         if ($err !== PAGER_OK) {
  72.             return $this->raiseError($this->errorMessage($err), $err);
  73.         }
  74.         $this->build();
  75.     }
  76.  
  77.     // }}}
  78.     // {{{ build()
  79.  
  80.     /**
  81.      * Generate or refresh the links and paged data after a call to setOptions()
  82.      *
  83.      * @access public
  84.      */
  85.     function build()
  86.     {
  87.         //reset
  88.         $this->_pageData = array();
  89.         $this->links = '';
  90.  
  91.         $this->_generatePageData();
  92.         $this->_setFirstLastText();
  93.  
  94.         $this->links .= $this->_getBackLink();
  95.         $this->links .= $this->_getPageLinks();
  96.         $this->links .= $this->_getNextLink();
  97.  
  98.         $this->linkTags .= $this->_getFirstLinkTag();
  99.         $this->linkTags .= $this->_getPrevLinkTag();
  100.         $this->linkTags .= $this->_getNextLinkTag();
  101.         $this->linkTags .= $this->_getLastLinkTag();
  102.     }
  103.  
  104.     // }}}
  105.     // {{{ getPageIdByOffset()
  106.  
  107.     /**
  108.      * Returns pageID for given offset
  109.      *
  110.      * @param $index Offset to get pageID for
  111.      * @return int PageID for given offset
  112.      */
  113.     function getPageIdByOffset($index)
  114.     {
  115.         if (!isset($this->_pageData)) {
  116.             $this->_generatePageData();
  117.         }
  118.  
  119.         if (($index % $this->_perPage) > 0) {
  120.             $pageID = ceil((float)$index / (float)$this->_perPage);
  121.         } else {
  122.             $pageID = $index / $this->_perPage;
  123.         }
  124.         return $pageID;
  125.     }
  126.  
  127.     // }}}
  128.     // {{{ getPageRangeByPageId()
  129.  
  130.     /**
  131.      * Given a PageId, it returns the limits of the range of pages displayed.
  132.      * While getOffsetByPageId() returns the offset of the data within the
  133.      * current page, this method returns the offsets of the page numbers interval.
  134.      * E.g., if you have pageId=3 and delta=10, it will return (1, 10).
  135.      * PageID of 8 would give you (1, 10) as well, because 1 <= 8 <= 10.
  136.      * PageID of 11 would give you (11, 20).
  137.      * If the method is called without parameter, pageID is set to currentPage#.
  138.      *
  139.      * @param integer PageID to get offsets for
  140.      * @return array  First and last offsets
  141.      * @access public
  142.      */
  143.     function getPageRangeByPageId($pageid = null)
  144.     {
  145.         $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
  146.         if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
  147.             // I'm sure I'm missing something here, but this formula works
  148.             // so I'm using it until I find something simpler.
  149.             $start = ((($pageid + (($this->_delta - ($pageid % $this->_delta))) % $this->_delta) / $this->_delta) - 1) * $this->_delta +1;
  150.             return array(
  151.                 max($start, 1),
  152.                 min($start+$this->_delta-1, $this->_totalPages)
  153.             );
  154.         } else {
  155.             return array(0, 0);
  156.         }
  157.     }
  158.  
  159.     // }}}
  160.     // {{{ getLinks()
  161.  
  162.     /**
  163.      * Returns back/next/first/last and page links,
  164.      * both as ordered and associative array.
  165.      *
  166.      * NB: in original PEAR::Pager this method accepted two parameters,
  167.      * $back_html and $next_html. Now the only parameter accepted is
  168.      * an integer ($pageID), since the html text for prev/next links can
  169.      * be set in the constructor. If a second parameter is provided, then
  170.      * the method act as it previously did. This hack's only purpose is to
  171.      * mantain backward compatibility.
  172.      *
  173.      * @param integer $pageID Optional pageID. If specified, links
  174.      *                for that page are provided instead of current one.
  175.      *                [ADDED IN NEW PAGER VERSION]
  176.      * @param  string $next_html HTML to put inside the next link
  177.      *                [deprecated: use the constructor instead]
  178.      * @return array Back/pages/next links
  179.      */
  180.     function getLinks($pageID=null, $next_html='')
  181.     {
  182.         //BC hack
  183.         if (!empty($next_html)) {
  184.             $back_html = $pageID;
  185.             $pageID    = null;
  186.         } else {
  187.             $back_html = '';
  188.         }
  189.  
  190.         if (!is_null($pageID)) {
  191.             $_sav = $this->_currentPage;
  192.             $this->_currentPage = $pageID;
  193.  
  194.             $this->links = '';
  195.             if ($this->_totalPages > $this->_delta) {
  196.                 $this->links .= $this->_printFirstPage();
  197.             }
  198.             $this->links .= $this->_getBackLink('', $back_html);
  199.             $this->links .= $this->_getPageLinks();
  200.             $this->links .= $this->_getNextLink('', $next_html);
  201.             if ($this->_totalPages > $this->_delta) {
  202.                 $this->links .= $this->_printLastPage();
  203.             }
  204.         }
  205.  
  206.         $back  = str_replace(' ', '', $this->_getBackLink());
  207.         $next  = str_replace(' ', '', $this->_getNextLink());
  208.         $pages = $this->_getPageLinks();
  209.         $first = $this->_printFirstPage();
  210.         $last  = $this->_printLastPage();
  211.         $all   = $this->links;
  212.         $linkTags = $this->linkTags;
  213.  
  214.         if (!is_null($pageID)) {
  215.             $this->_currentPage = $_sav;
  216.         }
  217.  
  218.         return array(
  219.             $back,
  220.             $pages,
  221.             trim($next),
  222.             $first,
  223.             $last,
  224.             $all,
  225.             $linkTags,
  226.             'back'  => $back,
  227.             'pages' => $pages,
  228.             'next'  => $next,
  229.             'first' => $first,
  230.             'last'  => $last,
  231.             'all'   => $all,
  232.             'linktags' => $linkTags
  233.         );
  234.     }
  235.  
  236.     // }}}
  237.     // {{{ _getPageLinks()
  238.  
  239.     /**
  240.      * Returns pages link
  241.      *
  242.      * @param $url  URL to use in the link
  243.      *              [deprecated: use the constructor instead]
  244.      * @return string Links
  245.      * @access private
  246.      */
  247.     function _getPageLinks($url = '')
  248.     {
  249.         //legacy setting... the preferred way to set an option now
  250.         //is adding it to the constuctor
  251.         if (!empty($url)) {
  252.             $this->_path = $url;
  253.         }
  254.  
  255.         //If there's only one page, don't display links
  256.         if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
  257.             return '';
  258.         }
  259.  
  260.         $links = '';
  261.         $limits = $this->getPageRangeByPageId($this->_currentPage);
  262.  
  263.         for ($i=$limits[0]; $i<=min($limits[1], $this->_totalPages); $i++) {
  264.             if ($i != $this->_currentPage) {
  265.                 $this->range[$i] = false;
  266.                 $this->_linkData[$this->_urlVar] = $i;
  267.                 $links .= $this->_renderLink($this->_altPage.' '.$i, $i);
  268.             } else {
  269.                 $this->range[$i] = true;
  270.                 $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  271.             }
  272.             $links .= $this->_spacesBefore
  273.                    . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
  274.         }
  275.         return $links;
  276.     }
  277.  
  278.     // }}}
  279. }
  280. ?>